package cn.itcast.hotel;

import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.service.IHotelService;
import cn.itcast.hotel.service.impl.HotelService;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.List;

public class EsDocumentDemo {
    @Autowired
    private IHotelService hotelService;

    private RestHighLevelClient client;

    @BeforeEach
    void setUp() {
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.150.101:9200")
        ));
    }

    @AfterEach
    void tearDown() throws IOException {
        this.client.close();
    }

    @Test
    public void getDocumentById () throws IOException {

        //1.
        GetRequest request = new GetRequest("hotel","36934");
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        String hotelJsonStr = response.getSourceAsString();
        HotelDoc hotelDoc = JSON.parseObject(hotelJsonStr, HotelDoc.class);
        System.out.println(hotelDoc);
    }
    @Test
    public void updateById () throws IOException {

        //1.创建修改文档的请求对象
        UpdateRequest request = new UpdateRequest("hotel","36934");
        request.doc("price","398");
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
        //3.解析响应得到文档结果
        System.out.println(response.getResult());
    }
    @Test
    public void deleteById () throws IOException {

        //1.创建修改文档的请求对象
        DeleteRequest request = new DeleteRequest("hotel","36934");

        //3.解析响应得到文档结果
    }
    @Test
    public void bulkAddDocument () throws IOException {

        // 批量查询酒店数据
        List<Hotel> list = hotelService.list();
        // 1.创建Request
        BulkRequest bulkRequest = new BulkRequest("hotel");
        // 2.准备参数，添加多个新增的Request
        for (Hotel hotel : list) {
            IndexRequest request = new IndexRequest();
            request.id(hotel.getId()+"");
            // 2.1.转换为文档类型HotelDoc
            HotelDoc hotelDoc = new HotelDoc(hotel);
            // 2.2.创建新增文档的Request对象
            request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
            bulkRequest.add(request);
        }
        // 3.发送请求
        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        //打印结果
        System.out.println(bulk.status());
    }

}


































